const app = getApp()
const db = wx.cloud.database()
const T = db.collection('user')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        src: null,
        src2:null,
        check3: false,
        type: null,
        specimen1: [{
            value: '血液'
        }, {
            value: '唾液'
        }],
        specimen:[],
        check: false,
        check2: false

    },
    onLoad: function (options) {
        this.setData({
            type: app.globalData.type
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //如已签名，则在签名栏显示签名
        if (app.globalData.signsrc != null) {
            this.setData({
                src: app.globalData.signsrc
            })
        }
        if (app.globalData.signsrc2 != null) {
            this.setData({
                src2: app.globalData.signsrc2
            })
        }
    },

    //知情同意书确认同意框
    radiocon: function (e) {
        this.setData({
            check3: !this.data.check3
        })
        console.log("用户打勾的是 ", this.data.check3)
    },
    //样本选择判定
    radioChange: function (e) {
        this.setData({
            specimen:e.detail.value
        })
        console.log('样本类型为:', this.data.specimen);
    },
    //保存按钮
    save: function () {
        //检测是否已签名及勾选同意框
        if (this.data.check3 == true && this.data.src != null) {
            console.log(this.data.src)
            if (app.globalData.type == 1) {
                wx.cloud.callFunction({
                    name: 'zqtys',
                    data: {
                        image: app.globalData.fileID,
                        name: app.globalData.name,
                        openid: app.globalData.openid
                    },
                    success: result => {
                        console.log('生成知情同意书word: ', result)
                        var wordid = result.result.fileID;
                        wx.cloud.callFunction({
                            name: 'userzqtys',
                            data: {
                                type: app.globalData.type,
                                openid: app.globalData.openid,
                                wordid: wordid,
                            },
                            success: resss => {
                                console.log(resss)
                            },
                            fail: resss => {
                                console.log(resss)
                            }
                        })
                    },
                    fail: result => {
                        console.log('生成知情同意书失败: ', result)
                    }
                })
            } else if (app.globalData.type == 2) {
                let specimen = this.data.specimen;
                wx.cloud.callFunction({
                    name: 'zqtys2',
                    data: {
                        specimen: specimen,
                        image: app.globalData.fileID,
                        name: app.globalData.name,
                        openid: app.globalData.openid
                    },
                    success: result => {
                        console.log('生成知情同意书word: ', result)
                        var wordid = result.result.fileID;
                        wx.cloud.callFunction({
                            name: 'userzqtys',
                            data: {
                                type: app.globalData.type,
                                openid: app.globalData.openid,
                                wordid: wordid,
                            },
                            success: resss => {
                                console.log(resss)
                            },
                            fail: resss => {
                                console.log(resss)
                            }
                        })
                    },
                    fail: result => {
                        console.log('生成知情同意书失败: ', result)
                    }
                })
            }
            else if (app.globalData.type == 3) {
                wx.cloud.callFunction({
                    name: 'zqtys3',
                    data: {
                        image: app.globalData.fileID,
                        image2:app.globalData.fileID2,
                        name: app.globalData.name,
                        openid: app.globalData.openid
                    },
                    success: result => {
                        console.log('生成知情同意书word: ', result)
                        var wordid = result.result.fileID;
                        wx.cloud.callFunction({
                            name: 'userzqtys',
                            data: {
                                type: app.globalData.type,
                                openid: app.globalData.openid,
                                wordid: wordid,
                            },
                            success: resss => {
                                console.log(resss)
                            },
                            fail: resss => {
                                console.log(resss)
                            }
                        })
                    },
                    fail: result => {
                        console.log('生成知情同意书失败: ', result)
                    }
                })
            }
            if (app.globalData.type == 1) {
                wx.navigateTo({
                    url: '/pages/wjdc/wjdc',
                })
            } else if (app.globalData.type == 2) {
                wx.navigateTo({
                    url: '/pages/wjdc2/wjdc2',
                })
            }
            else if (app.globalData.type == 3) {
                wx.navigateTo({
                    url: '/pages/QRcode/QRcode',
                })
            }
        } else {
            wx.showToast({
                title: '请签名并确认!',
                icon: 'none',
                duration: 1500
            })
        }
    }
})